package com.siashan.unipay4j.ali.splitbill;

import com.alibaba.fastjson.JSON;
import com.alipay.api.request.AlipayTradeOrderSettleRequest;
import com.alipay.api.response.AlipayTradeOrderSettleResponse;
import com.siashan.unipay4j.ali.AbstractAliPay;
import com.siashan.unipay4j.core.IPayConfig;
import com.siashan.unipay4j.core.Result;
import com.siashan.unipay4j.core.exception.Unipay4jInvalidParamException;
import com.siashan.unipay4j.core.splitbill.ISplitBill;
import com.siashan.unipay4j.core.splitbill.ProfitModel;
import com.siashan.unipay4j.core.splitbill.Receiver;
import com.siashan.unipay4j.core.splitbill.SplitBillResp;
import com.siashan.unipay4j.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 支付宝商家分账
 * 商家分账场景下分账收入方 trans_in 只支持支付宝账户，不支持使用 cardAliasNo 卡编号。
 *
 * @author siashan
 * @since v1.0.1
 **/
@Slf4j
public class AliPaySplitBill extends AbstractAliPay implements ISplitBill {
    public AliPaySplitBill() {
    }

    public AliPaySplitBill(IPayConfig payConfig) {
        super(payConfig);
    }

    /**
     * 分账
     *
     * @param model  请求实体
     * @return 分账结果
     */
    @Override
    public Result<SplitBillResp> split(ProfitModel model) {
        try {
            AlipayTradeOrderSettleRequest request = new AlipayTradeOrderSettleRequest();
            request.setBizContent(JSON.toJSONString(wrapParam(model)));
            AlipayTradeOrderSettleResponse r = alipayClient.execute(request);
            if (r.isSuccess()) {
                return Result.tradeSuccess(wrapResp(r));
            }
            return Result.tradeFail(r.getSubCode(),r.getSubMsg());

        } catch (Exception e) {
            log.warn("AliPaySplitBill => 订单{} 支付宝分账请求异常", model.getOutTradeNo());
        }
        return Result.exception();
    }



    private SplitBillResp wrapResp(AlipayTradeOrderSettleResponse r){
        SplitBillResp resp = new SplitBillResp();
        resp.setTradeNo(r.getTradeNo());
        return resp;
    }


    /**
     * 封装请求参数
     *
     * @param model 请求实体
     * @return 请求参数
     */
    private Map<String, Object> wrapParam(ProfitModel model) {
        Map<String, Object> map = new HashMap<>();
        List<Receiver> receivers = model.getReceivers();
        if (null == receivers || receivers.isEmpty()) {
            throw new Unipay4jInvalidParamException("分账接受方不能为空");
        }

        List<AliReveiver> list = new ArrayList<>();
        AliReveiver reveiver = null;
        for (Receiver r : receivers) {
            reveiver = new AliReveiver();
            reveiver.setAmount(r.getAmount());
            if (StrUtil.isNotEmpty(r.getDescription())) {
                reveiver.setDesc(r.getDescription());
            }
            reveiver.setTransIn(r.getAccount());
            list.add(reveiver);
        }
        map.put("trade_no", model.getTradeNo());
        map.put("out_request_no", model.getOutTradeNo());
        map.put("royalty_parameters", list);
        return map;
    }
}
